/**
 *
 */
$(document).ready(function() {
    $("form").submit(function() {
        return false;
    });

    var VAL = {
        EVT_USER : $("#userId").text() ,
        T_FTPTOLALARM : "FTPTOLALARM",
        T_FBPBISTOL   : 'FBPBISTOL',
        T_FBPBISDAT   : 'FBPBISDAT',
        NORMAL : "0000000",
        DISABLED_ATTR : {
            "disabled" : true
        },
        ENABLED_ATTR : {
            "disabled" : false
        }
    };

    var domObj = {
        $startTimestamp : $("#startTimestamp"),
        $endTimestamp : $("#endTimestamp"),
        $startIn : $("#startIn"),
        $endIn : $("#endIn"),
        $lineIdFk :$("#lineIdFk"),
        $fabIdFk :$("#fabIdFk"),
        $toolIdFk : $("#toolIdFk"),
        $alarmDealUsr : $("#alarmDealUsr"),
       $alarmCode : $("#alarmCode"),
        $alarmEvtSeqId : $("#alarmEvtSeqId"),
        $alarmLvl : $("#alarmLvl"),
//        $alarmEvtTyp :$("#alarmEvtTyp"),
        $alarmDsc : $("#alarmDsc"),
        $alarmNote : $("#alarmNote"),
        $alarmDealMeas : $("#alarmDealMeas"),
        $alarmDealResult : $("#alarmDealResult"),
        $featActUsr : $("#featActUsr"),
        $featAction : $("#featAction"),

        buttons : {
            $query_btn : $("#query_btn"),
            $clear_btn : $("#clear_btn"),
            $del_btn : $("#del_btn"),
            $commit_btn : $("#commit_btn"),
        },
        grids : {
            $toolAlarmDiv : $("#toolAlarmDiv"),
            $toolAlarmGrd : $("#toolAlarmGrd"),
            $toolAlarmPg : $("#toolAlarmPg")
        },
    };

    function initToolAlarm() {
        domObj.grids.$toolAlarmGrd.jqGrid({
            url : "",
            datatype : "local",
            mtype : "POST",
            height : 500,
            width : $("#toolAlarmDiv").width() * 0.99,
            autowidth : true,
            shrinkToFit:false,
            autoScroll: false,
            resizable : true,
            loadonce : true,
            viewrecords : true,
            rownumbers : true,
            rowNum : 20,
            emptyrecords : true,
            colModel : [ 
                {name : 'evt_seq_id', index : 'evt_seq_id', label : '', width : 140, hidden:true},
				{name : 'start_timestamp', index : 'start_timestamp', label : '报警开始时间', width : 140},
				{name : 'end_timestamp', index : 'end_timestamp', label : '报警处理时间', width : 140},
                {name : 'fab_id_fk', index : 'fab_id_fk', label : '厂别', width : 150},
				{name : 'line_id_fk', index : 'line_id_fk', label : '线别', width : 150},
				{name : 'tool_id_fk', index : 'tool_id_fk', label : '设备代码', width : 150}, 
				{name : 'alarm_deal_usr', index : 'alarm_deal_usr', label : '警报处理者', width : 150}, 
				{name : 'alarm_code', index : 'alarm_code', label : '警报代码', width : 150}, 
				{name : 'alarm_lvl', index : 'alarm_lvl', label : '警报等级', width : 150}, 
				{name : 'alarm_evt_typ', index :'alarm_evt_typ', label : '警报类型', width : 150}, 
				{name : 'alarm_dsc', index : 'alarm_dsc', label : '警报内容', width : 150},
				{name : 'alarm_note', index : 'alarm_note', label : '警报备注信息', width : 150},
				{name : 'alarm_deal_meas', index : 'alarm_deal_meas', label : '警报处理措施', width : 150},
				{name : 'alarm_deal_result', index : 'alarm_deal_result', label : '警报处理结果结果', width : 150}
            ],
            pager : "toolAlarmPg",
            onSelectRow : function(rowId) {
            	updateFn();
                var rowData = domObj.grids.$toolAlarmGrd.jqGrid("getRowData", rowId);
                inObj = {
                    trx_id : VAL.T_FTPTOLALARM,
                    action_flg : 'I',
                    evt_seq_id : rowData.evt_seq_id
                    
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    SelectDom.setSelect(domObj.$lineIdFk, outObj.oary[0].line_id_fk);
                    SelectDom.setSelect(domObj.$fabIdFk, outObj.oary[0].fab_id_fk);
                    SelectDom.setSelect(domObj.$toolIdFk, outObj.oary[0].tool_id_fk);
                   SelectDom.setSelect(domObj.$alarmCode, outObj.oary[0].alarm_code);
                    SelectDom.setSelect(domObj.$alarmLvl, outObj.oary[0].alarm_lvl);
                    $("#startTimestamp input").val(outObj.oary[0].start_timestamp);
                    $("#endTimestamp input").val(outObj.oary[0].end_timestamp);
                    domObj.$alarmEvtSeqId.val(outObj.oary[0].evt_seq_id);
                    domObj.$alarmDealUsr.val(outObj.oary[0].alarm_deal_usr);
                    domObj.$alarmNote.val(outObj.oary[0].alarm_note);
                    domObj.$alarmDsc.val(outObj.oary[0].alarm_dsc);
                    domObj.$alarmDealMeas.val(outObj.oary[0].alarm_deal_meas);
                    domObj.$alarmDealResult.val(outObj.oary[0].alarm_deal_result);
                    domObj.$featAction.val(outObj.oary[0].feat_action);
                    domObj.$featActUsr.val(outObj.oary[0].feat_act_usr);
                }
            }
        });
    } ;
    
    var baseFnc = {
        convertTimestamp2Date : function(timestamp) {
            var date = new Date(timestamp.substring(0, 10));
            return date;
        }
    };

    // 初始化下拉框
    var initSelectFnc = {
        // 1.初始化设备代码
        initToolSel : function() {
            var fabId =  $.trim($("#fabIdFk").val());
            var lineId =   $.trim(domObj.$lineIdFk.val());
            var iary2  = {
                fab_id_fk : fabId,
                bay_id :lineId
            };
            domObj.$toolIdFk.empty();
            // var bay_area = domObj.$lineIdFk.find("option:selected").val();
            var inObj = {
                trx_id: VAL.T_FBPBISTOL,
                action_flg: "Q",
                iary: [iary2]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$toolIdFk, outObj.oary, "tool_id", "tool_id", "", true);
            }
        },

        // 2.初始化线别
        initLineSel : function (){
            comAddValueByDataCateFnc(domObj.$lineIdFk,"AREA","data_ext","data_desc",true);
        },

        // 4.初始化警报级别
        initAlarmLvl : function() {
            comAddValueByDataCateFnc(domObj.$alarmLvl,"AMLV","data_ext","data_desc",true);
        },

        // 5.初始化警报类型
//        initAlarmType : function() {
//            var iary,inObj, outObj;
//            iary ={
//                data_cate : "AMTY"
//            };
//            inObj = {
//                trx_id : VAL.T_FBPBISDAT,
//                action_flg : "Q",
//                iary : [ iary ]
//            };
//            outObj = comTrxSubSendPostJson(inObj);
//            if (outObj.rtn_code === VAL.NORMAL) {
//                var tblCnt = outObj.tbl_cnt;
//                SelectDom.initWithSpace(domObj.$alarmEvtTyp);
//                var warp = document.createDocumentFragment();
//                for (i = 0; i < tblCnt; i++) {
//                    var oary = tblCnt > 1 ? outObj.oary[i]
//                        : outObj.oary[0];
//                    var option = document.createElement("option");
//                    option.value = oary.data_ext;
//                    option.text = oary.data_desc;
//                    warp.appendChild(option);
//                }
//                domObj.$alarmEvtTyp.append(warp);
//            }
//        },

        //初始化开始时间，结束时间。
        iniDateTimePicker : function() {

            laydate.render({
                elem: '#startIn',
                type: 'datetime'
            });
            laydate.render({
                elem: '#endIn',
                type: 'datetime'
            });
        },

        initFabIdSel : function (){
            comAddValueByDataCateFnc("#fabIdFk","FBID","data_ext","data_desc",true);
        },

        initAlarmSel : function (){
            comAddValueByDataCateFnc("#alarmCode","AMCD","data_ext","data_desc",true);
        },

    };
    
    var buttonFnc = {
        queryBtnfunc: function () {
            var startTimestamp = $.trim(domObj.$startIn.val());
            var endTimestamp = $.trim(domObj.$endIn.val());
            var lineIdFk = $.trim(domObj.$lineIdFk.val());
            var toolIdFk = $.trim(domObj.$toolIdFk.val());
            var alarmDealUsr = $.trim(domObj.$alarmDealUsr.val());
//            var alarmCode = $.trim(domObj.$alarmCode.val());
            var alarmLvl = $.trim(domObj.$alarmLvl.val());
//          var alarmEvtTyp = domObj.$alarmEvtTyp.val();

            if(!startTimestamp){
                showErrorDialog("", "请输入开始时间！");
                return false;
            }
            if (!endTimestamp) {
                showErrorDialog("", "请输入结束时间！");
                return false;
            }
            if (baseFnc.convertTimestamp2Date(startTimestamp) > baseFnc
                    .convertTimestamp2Date(endTimestamp)) {
                showErrorDialog("", "开始时间不能在结束时间之后!");
                return false;
            }
            iary = {
                start_timestamp: startTimestamp,
                end_timestamp: endTimestamp
            };
            
//            if (alarmCode) {
//            	iary.alarm_code = alarmCode;
//            }
            if (alarmDealUsr) {
                iary.alarm_deal_usr = alarmDealUsr;
            }
            if (alarmLvl) {
                iary.alarm_lvl = alarmLvl;
            }

            var inObj = {
                trx_id: VAL.T_FTPTOLALARM,
                action_flg: 'Q',
                iary: [iary]
            };

            if (lineIdFk) {
            	inObj.line_id_fk = lineIdFk;
            }
            
            if (toolIdFk) {
            	inObj.tool_id_fk = toolIdFk;
            }
            
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oary, "#toolAlarmGrd");
            }
        },
        clearBtnfunc: function () {
            enableFn();
            SelectDom.initWithSpace(domObj.$toolIdFk);
//            SelectDom.initWithSpace(domObj.$alarmCode);
            SelectDom.initWithSpace(domObj.$alarmLvl);
//            SelectDom.initWithSpace(domObj.$alarmEvtTyp);
            SelectDom.initWithSpace(domObj.$lineIdFk);
            init();
        },
        commitBtnfunc: function () {
            var startTimestamp = $("#startIn").val();

            var endTimestamp = $("#endIn").val();
            var fabIdFk = $.trim(domObj.$fabIdFk.val());
            var lineIdFk = $.trim(domObj.$lineIdFk.val());
            var toolIdFk = $.trim(domObj.$toolIdFk.val());
            var alarmDealUsr = $.trim(domObj.$alarmDealUsr.val());
//            var alarmCode = $.trim(domObj.$alarmCode.val());
            var alarmEvtSeqId = $.trim(domObj.$alarmEvtSeqId.val());
            var alarmLvl = $.trim(domObj.$alarmLvl.val());
            var alarmDsc = $.trim(domObj.$alarmDsc.val());
            var alarmNote = $.trim(domObj.$alarmNote.val());
            var alarmDealMeas = $.trim(domObj.$alarmDealMeas.val());
            var alarmDealResult = $.trim(domObj.$alarmDealResult.val());
            var featAction = $.trim(domObj.$featAction.val());
            var featActUsr = $.trim(domObj.$featActUsr.val());

            if(!startTimestamp){
                showErrorDialog("", "请输入开始时间！");
                return false;
            }
            if (!endTimestamp) {
                showErrorDialog("", "请输入结束时间！");
                return false;
            }
            if (baseFnc.convertTimestamp2Date(startTimestamp) > baseFnc.convertTimestamp2Date(endTimestamp)) {
                showErrorDialog("", "开始时间不能在结束时间之后!");
                return false;
            }
            if (!toolIdFk) {
                showErrorDialog("", "请选择设备代码！");
                return false;
            }
            if(!fabIdFk){
                showErrorDialog("", "请选择厂别！");
                return false;
            }
//            if (!alarmCode) {
//                showErrorDialog("", "请选择警报代码！");
//                return false;
//            }
            if (!alarmDealUsr) {
                showErrorDialog("", "请输入警报处理者！");
                return false;
            }
            if (!alarmLvl) {
                showErrorDialog("", "请选择警报级别！");
                return false;
            }
            if (!alarmDsc) {
                showErrorDialog("", "请输入警报内容！");
                return false;
            }

            var iary = {
                start_timestamp: startTimestamp,
                end_timestamp: endTimestamp,
                alarm_deal_usr: alarmDealUsr,
                alarm_code: "AM01",
                alarm_lvl: alarmLvl,
                alarm_note: alarmNote,
                alarm_dsc: alarmDsc,
                alarm_deal_meas: alarmDealMeas,
                alarm_deal_result: alarmDealResult,
                feat_action : featAction,
                feat_act_usr : featActUsr
            };
            var inObj = {
                trx_id: VAL.T_FTPTOLALARM,
                action_flg: 'A',
                opi_ope_flg: 'Y',
                alarm_evt_typ: 'O',//OPI登记
                evt_user: VAL.EVT_USER,
                line_id_fk: lineIdFk,
                tool_id_fk: toolIdFk,
            	fab_id_fk : fabIdFk,
            	evt_seq_id: alarmEvtSeqId,
                iary: [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
            	buttonFnc.queryBtnfunc();
            	showSuccessDialog("报警记录登记成功");
            }
        },
        cancelBtnfunc: function () {
            var selRowId, rowData, iary, inObj, outObj;
            //获得选中的行ID
            var selRowId = domObj.grids.$toolAlarmGrd.jqGrid("getGridParam", "selrow");
            if (!selRowId) {
                showErrorDialog("", "请选择需要删除警报信息！");
                return false;
            }
            $("#del_btn").showCallBackWarnningDialog({
                errMsg: "是否删除警报记录,请确认!!!!",
                callbackFn: function (data) {
                    if (data.result == true) {
                        //获得选中行的数据
                        var rowData = domObj.grids.$toolAlarmGrd.jqGrid("getRowData", selRowId);
                        var inObj = {
                            trx_id: VAL.T_FTPTOLALARM,
                            action_flg: "D",
                            evt_seq_id: rowData.evt_seq_id,
                        };
                        var outObj = comTrxSubSendPostJson(inObj);
                        if (outObj.rtn_code === VAL.NORMAL) {
                            showSuccessDialog("删除警报记录成功");
                            domObj.grids.$toolAlarmGrd.jqGrid( "delRowData", selRowId);
                        }
                    }
                }
            });
        }
    };
    
    function init() {
        initSelectFnc.iniDateTimePicker();
        initSelectFnc.initLineSel();
        initSelectFnc.initToolSel();
        initSelectFnc.initFabIdSel();
        initSelectFnc.initAlarmSel();
//        initSelectFnc.initAlarmSel();
        initSelectFnc.initAlarmLvl();
//        initSelectFnc.initAlarmType();
        domObj.$alarmEvtSeqId.val("");
        domObj.$alarmDealUsr.val("");
        domObj.$alarmDsc.val("");
        domObj.$alarmNote.val("");
        domObj.$alarmDealMeas.val("");
        domObj.$alarmDealResult.val("");
        domObj.$featActUsr.val("");
        domObj.$featAction.val("");
    }
    $("#fabIdFk").on("change",initSelectFnc.initToolSel);
    domObj.$lineIdFk.on("change", initSelectFnc.initToolSel);
    //将控件变成不可修改
    function unableFn() {
        domObj.$startIn.attr(VAL.DISABLED_ATTR);
        domObj.$lineIdFk.attr(VAL.DISABLED_ATTR);
        domObj.$toolIdFk.attr(VAL.DISABLED_ATTR);
//        domObj.$alarmCode.attr(VAL.DISABLED_ATTR);
    }
    
    //将控件变成都可修改
    function enableFn() {
        domObj.$startIn.attr(VAL.ENABLED_ATTR);
        domObj.$lineIdFk.attr(VAL.ENABLED_ATTR);
        domObj.$toolIdFk.attr(VAL.ENABLED_ATTR);
//        domObj.$alarmCode.attr(VAL.ENABLED_ATTR);
    }
    
    function updateFn(){
        //以下五个控件不可以被修改
        domObj.$startIn.attr(VAL.DISABLED_ATTR);
        domObj.$lineIdFk.attr(VAL.DISABLED_ATTR);
        domObj.$fabIdFk.attr(VAL.DISABLED_ATTR);
        domObj.$toolIdFk.attr(VAL.DISABLED_ATTR);
       domObj.$alarmCode.attr(VAL.DISABLED_ATTR);
        //以下都是可以被修改的

        domObj.$alarmDealUsr.attr(VAL.ENABLED_ATTR);
        domObj.$alarmLvl.attr(VAL.ENABLED_ATTR);
//        domObj.$alarmEvtTyp.attr(VAL.ENABLED_ATTR);
        domObj.$alarmNote.attr(VAL.ENABLED_ATTR);
        domObj.$alarmDsc.attr(VAL.ENABLED_ATTR);
        domObj.$alarmDealMeas.attr(VAL.ENABLED_ATTR);
        domObj.$alarmDealResult.attr(VAL.ENABLED_ATTR);
        domObj.$featActUsr.attr(VAL.ENABLED_ATTR);
        domObj.$featAction.attr(VAL.ENABLED_ATTR);
    }

    //页面初始化函数
    function pageInit() {
        initToolAlarm();
        buttonFnc.clearBtnfunc();
        domObj.buttons.$query_btn.bind('click',buttonFnc.queryBtnfunc);//根据条件查询报警信息
        domObj.buttons.$clear_btn.bind('click',buttonFnc.clearBtnfunc);//清空界面初始化
        domObj.buttons.$commit_btn.bind('click',buttonFnc.commitBtnfunc);//登记报警备注信息以及处理结果信息
        domObj.buttons.$del_btn.bind('click',buttonFnc.cancelBtnfunc);//删除报警信息
    }
    
    pageInit();
            
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth;
        divWidth = $("#toolAlarmDiv").width();
        offsetBottom =  $(window).height() - $("#toolAlarmDiv").offset().top;
        $("#toolAlarmDiv").height(offsetBottom * 0.95);
        $("#toolAlarmGrd").setGridWidth(divWidth * 0.99);
        $("#toolAlarmGrd").setGridHeight(offsetBottom * 0.99 - 101);
    };
    
    resizeFnc();
    
    $(window).resize(function() {
        resizeFnc();
    });
});
